import java.util.Arrays;

public class MyArraylist implements IList{
    public static final int LEN=2;
    public  int[] arr;
    public int usesize=0;
    public MyArraylist(){
        this.arr=new int[LEN];
    }
    public void chekCapacity(){
        arr= Arrays.copyOf(arr,arr.length*2);

    }

//    public  int[] check(int[] arr){
//        if (isFull()){
//            arr= Arrays.copyOf(arr,2*arr.length);
//
//        }
//        return arr;
//    }
    @Override
    public void add(int data) {
        //check;
        if(isFull()){

            chekCapacity();
        }
        arr[usesize]=data;
        usesize++;
    }


    @Override
    public void add(int pos, int data) {
       if(isFull()) {
            chekCapacity();
        }
    if(pos<0||pos>=arr.length){
        System.out.println("位置不合法");
    }else {
        for(int i=usesize-1;i>=pos;i--){
            arr[i+1]=arr[i];
        }
        arr[pos]=data;
    }
    usesize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i=0;i<usesize;i++){
            if(arr[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i=0;i<usesize;i++){
            if(arr[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        return arr[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0||pos>usesize){
            System.out.println("位置不合法");
        }
        arr[pos]=value;
    }

    @Override
    public void remove(int toRemove) {
    if(isEmpty()){
        System.out.println("已为空，不能删除");
    }else {
        int pos=indexOf(toRemove);
        for(int i=pos;i<usesize-1;i++){
            arr[i]=arr[i+1];
        }
    }
    usesize--;
    }

    @Override
    public int size() {
        return usesize;
    }

    @Override
    public void clear() {
        usesize = 0;
    }
    @Override
    public void display() {
    for (int i=0;i<usesize;i++){
        System.out.print(arr[i]+" ");
    }
    }

    @Override
    public boolean isFull() {
        return usesize==arr.length;
    }

    @Override
    public boolean isEmpty() {
        return usesize==0;
    }
}
